package top.lixunda.ecommerce.server.business.utils;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import top.lixunda.commerce.common.core.entity.MessageObject;
import top.lixunda.ecommerce.server.business.api.entity.dto.PageDTO;
import top.lixunda.ecommerce.server.user.api.entity.db.AccountDO;
import top.lixunda.ecommerce.server.user.api.entity.db.UserDetailDO;
import top.lixunda.ecommerce.server.user.api.entity.dto.AccountQueryDTO;
import top.lixunda.ecommerce.server.user.api.feign.IFeignAccountController;

import javax.annotation.PostConstruct;
import java.util.List;

/**
 * IFeignAccountController 解决循环依赖问题
 *
 * @author lixunda
 * @version time:2020/4/20 10:17
 */
@SuppressWarnings("unused")
@Slf4j
@Component
public class AccountControllerUtil {

    @SuppressWarnings("SpringJavaAutowiredFieldsWarningInspection")
    @Autowired
    private IFeignAccountController feignAccountController;

    private static volatile IFeignAccountController accountController;

    @PostConstruct
    public void init() {
        accountController = feignAccountController;
        log.debug("注入IFeignAccountController到AccountControllerUtil ：{} <= {}", accountController, feignAccountController);
    }

    /**
     * 根据id查询账户数据
     *
     * @param accountId 账户编号
     * @return 账户信息
     */
    public static MessageObject<AccountDO> queryAccountById(String accountId) {
        return accountController.queryAccountById(accountId);
    }

    /**
     * 条件查询账户数据
     *
     * @param accountQueryDTO 条件查询信息
     * @return 账户列表
     */
    public static MessageObject<List<AccountDO>> queryAccountByWrapper(AccountQueryDTO accountQueryDTO) {
        return accountController.queryAccountByWrapper(accountQueryDTO);
    }

    /**
     * 分页条件查询用户数据
     *
     * @param pageDTO 添加分页数据
     * @return 账户分页数据
     */
    public static MessageObject<Page<AccountDO>> queryAccountByPageWrapper(PageDTO<AccountDO, AccountQueryDTO> pageDTO) {
        return accountController.queryAccountByPageWrapper(pageDTO);
    }

    /**
     * 使用token进行登录
     *
     * @param token token字符串
     * @return 返回去除密码账户信息，登陆失败时返回Object为Null
     */
    public static MessageObject<UserDetailDO> loginByTokenString(String token) {
        return accountController.loginByTokenString(token);
    }

}
